//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.sailing.xjpb.common.orika;

import ma.glasnost.orika.impl.generator.SourceCodeContext;
import ma.glasnost.orika.impl.generator.VariableRef;
import ma.glasnost.orika.impl.generator.specification.AbstractSpecification;
import ma.glasnost.orika.metadata.FieldMap;

import java.util.Date;

/**
 * 对象和日期转换
 *
 * @author YW
 */
public class ObjectToDate extends AbstractSpecification {

    @Override
    public boolean appliesTo(FieldMap fieldMap) {
        return Date.class.isAssignableFrom(fieldMap.getBType().getRawType());
    }

    @Override
    public String generateMappingCode(FieldMap fieldMap, VariableRef source, VariableRef destination, SourceCodeContext code) {
        if (code.isDebugEnabled()) {
            code.debugField(fieldMap, "converting " + source.type() + " to Date ");
        }

        String assignDate = destination.assign("com.sailing.xjpb.common.DateUtils.toDate(%s)", source);
        String mapNull = shouldMapNulls(fieldMap, code) ? String.format(" else {\n %s;\n}", destination.assignIfPossible("null")) : "";
        return SourceCodeContext.statement("%s { %s; } %s", source.ifNotNull(), assignDate, mapNull);
    }

}
